// 计算均线
function calcLine(dataList) {
    let params = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 144]
    // let params = [5, 15,25,35,45,55,65,70,75,80,85,90,95,100,100,100,200]
    // let params = [5, 20,35,50,65,80,95,110,125,140,155,170,185,200,215,230,300]


    let closeSum = 0;
    const emaValues: number[] = [];
    let dataArr: any = []
    let tempArr: any = []
    let color = []
    dataList.map((kLineData: any, i: number) => {
        const ema: any = [];
        const close = kLineData.close;
        closeSum += close;
        tempArr[i] = []
        params.forEach((p: number, index: number) => {
            if (i >= p - 1) {
                if (i > p - 1) {
                    emaValues[index] = (2 * close + (p - 1) * emaValues[index]) / (p + 1);
                } else {
                    emaValues[index] = closeSum / p;
                }
                ema.push(emaValues[index])
                tempArr[i].push(emaValues[index])
                // console.log(tempArr[i][index], tempArr[i - 1][index])
                if (tempArr[i][index] < tempArr[i - 1][index]) {
                    color.push(1)
                } else {
                    color.push(0)
                }
            }
        });
        // kLineData.trend1=ema['ema18']
        dataArr.push(ema)

        // kLineData.trend2=ema['ema19']

    });
    dataArr = insArray(replaceEmptyWithNaN(tempArr), color)
    // console.log(dataArr)
    window.WTTLineData = replaceEmptyWithNaN(dataArr);

    return dataArr;
    // console.log(tempArr)
    // return tempArr;

}
// 插入数组
function insArray(originalArray: any, insertArray: any) {
    let resultArray: any = [];

    originalArray.forEach((innerArray) => {
        let tempArray: any = [];
        innerArray.forEach((value) => {
            tempArray.push(value);
            if (insertArray.length > 0) {
                tempArray.push(insertArray.shift());
            }
        });
        resultArray.push(tempArray);
    });
    return resultArray;
}
function replaceEmptyWithNaN(arr) {
    return arr.map((value) => {
        if (Array.isArray(value)) {
            // 递归处理嵌套数组
            return replaceEmptyWithNaN(value);
        }
        // 替换空值为 NaN
        return value === '' ? NaN : value;
    });
}
export { calcLine }